import React, { useState, useEffect } from 'react';
import { Table, Button, notification } from 'antd';
import SearchList from '@/components/SearchList';
import {
  EyeFilled,
  PlusCircleFilled,
  EditOutlined,
  DeleteFilled,
} from '@ant-design/icons';
// import CreateModal from './createModal';
import Check from './checkOrg';
// import Edit from './editOrg';
import API from '@/api';
import { transFormIndex, showDeleteConfirm } from '@/utils/util';
import { connect, useDispatch } from 'umi';
const organizationManagement = ({ orgMap, orgOptions }) => {
  const [list, setList] = useState([]);
  const [visible, setVisible] = useState(false);
  const [visibleEdit, setVisibleEdit] = useState(false);
  const [id, setId] = useState(null);
  const [status, setStatus] = useState(0);
  const [pagination, setPagination] = useState({
    showQuickJumper: true,
  });
  const searchList = [
    {
      name: 'name',
      title: '组织名称',
      type: 'input',
    },
    // {
    //   name: 'type',
    //   title: '组织属性',
    //   type: 'select',
    //   otherProps: {
    //     options: orgOptions,
    //   },
    // },
  ];
  const [params, setParams] = useState({
    pageNo: 1,
    pageSize: 10,
  });
  const initList = (data = {}) => {
    const values = { ...params, ...data };
    API.org_selectOrgListPage(values).then((res) => {
      if (res?.t) {
        setList(res.t.queryList);
        setPagination({
          ...pagination,
          total: res.t.totalSize,
          current: res.t.currentPage,
          pageSize: res.t.pageSize,
        });
      }
    });
  };
  const changeTable = (pagination) => {
    setParams({
      ...params,
      pageNo: pagination.current,
      pageSize: pagination.pageSize,
    });
  };

  const delFn = (id) => {
    API.org_delById({ id }).then((res) => {
      if (res) {
        notification.success({
          message: '删除成功',
        });
        initList();
      }
    });
  };
  const dispatch = useDispatch();
  if (!orgOptions) {
    dispatch({
      type: 'dict/queryDict',
      payload: {
        dictCode: 'ZZLX',
      },
    });
  }
  useEffect(() => {
    initList(params);
  }, [params]);
  const searchFn = (data) => {
    setParams({
      ...data,
      pageNo: 1,
      pageSize: 10,
    });
  };
  const columns = [
    {
      title: '序号',
      dataIndex: 'Index',
      align: 'center',
      render: (text, r, index) => transFormIndex(pagination, index),
    },
    {
      title: '组织名称',
      dataIndex: 'name',
      align: 'center',
    },
    {
      title: '组织属性',
      dataIndex: 'type',
      align: 'center',
      render: (text) => orgMap[text],
    },
    {
      title: '上级组织',
      dataIndex: 'parentOrgName',
      align: 'center',
      render: (text) => text || '-',
    },
    {
      title: '组织描述',
      dataIndex: 'info',
      align: 'center',
    },
    {
      title: '操作',
      dataIndex: 'Operation',
      align: 'center',
      render: (text, r) => (
        <div>
          <Button
            type="link"
            icon={<EyeFilled />}
            className="table-icon"
            onClick={() => showDetail(r.id)}
          ></Button>
          {/* 7月5号去掉编辑功能 */}
          {/* <Button
            type="link"
            icon={<EditOutlined />}
            className="table-icon"
            onClick={() => editFn(r.id)}
          ></Button> */}
          {/* <Button
            type="link"
            icon={<DeleteFilled />}
            className="table-icon"
            onClick={() => showDeleteConfirm(delFn.bind(null, r.id))}
          ></Button> */}
        </div>
      ),
    },
  ];
  const showDetail = (id) => {
    setVisible(true);
    setId(id);
  };
  const createLink = () => {
    setVisible(true);
  };
  const cancelFn = () => {
    setVisibleEdit(false);
    setVisible(false);
    setId(null);
  };
  const editFn = (id) => {
    setVisibleEdit(true);
    setId(id);
  };
  const onCreate = (values) => {
    const request = API.org_updateById; //API.org_insert
    values.id = id;
    request(values).then((res) => {
      if (res) {
        notification.success({
          message: '编辑成功',
        });
        setVisibleEdit(false);
        initList();
      }
    });
  };
  return (
    <>
      <SearchList searchList={searchList} initList={searchFn}></SearchList>
      <div className="bgf main-x">
        {/* <div className="tools-x">
          <Button
            type="link"
            icon={<PlusCircleFilled />}
            className="btn-icon"
            onClick={createLink}
          >
            新增
          </Button>
        </div> */}
        <Table
          columns={columns}
          rowKey="id"
          dataSource={list}
          onChange={changeTable}
          className="custom-table"
          pagination={pagination}
        ></Table>
      </div>
      <Check
        visible={visible}
        onCreate={onCreate}
        onCancel={cancelFn}
        id={id}
      ></Check>
      {/* <Edit
        visible={visibleEdit}
        onCreate={onCreate}
        onCancel={cancelFn}
        id={id}
      ></Edit> */}
    </>
  );
};

export default connect(({ dict }) => ({
  orgOptions: dict.orgOptions,
  orgMap: dict.orgMap,
}))(organizationManagement);
